package com.sxit.ecwx.core.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
 * 
 * 功能说明:httpclient工具类 
 * 创建时间: 2012-12-12上午11:14:17
 * @author admin
 */
public class HttpClientUtil {
	/**
	 * 
	 * 功能说明:get请求   
	 * @param: @param url
	 * @param: @return      
	 * @return: String     
	 * @author:admin 
	 * @date: 2012-12-12 上午11:14:08
	 */
	public static String get(String url) {
		String res = null;
		DefaultHttpClient httpclient = new DefaultHttpClient();
		//httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
		try {
			HttpGet httpget = new HttpGet(url);
			System.out.println("executing request" + httpget.getRequestLine());
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			System.out.println("----------------------------------------");
			System.out.println(response.getStatusLine());
			if (entity != null) {
				System.out.println("Response content length: "
						+ entity.getContentLength());
			}
			res=new String(EntityUtils.toString(entity, "utf-8").getBytes("ISO-8859-1"),"utf-8");
			System.out.println("executing result:"+res);
			System.out.println("----------------------------------------");
			EntityUtils.consume(entity);
		}catch(Exception e){
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return res;
	}
	
	 /**
     * post请求带xml数据
     * 
     * @param url
     * @param obj
     * @return
     */
    public static String post(String url, String xml)
    {
    	DefaultHttpClient client = new DefaultHttpClient();

        HttpPost request = new HttpPost(url);
        try
        {
            StringEntity entity = new StringEntity(xml.toString());
            // 创建POST请求
            request.setEntity(entity);
            // 发送请求
            HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
            {
                throw new RuntimeException("请求失败");
            }
            HttpEntity resEntity = response.getEntity();
            String result = (resEntity == null) ? null : EntityUtils.toString(resEntity, "UTF-8");
            return result;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

	
	public static void main(String[] args) {
		System.out.println(get("http://client.10010.com/clientT/login.do?version=iphone_c@1.4.1&mobile=18605910802&password=123456&version=iphone_c@1.4.1"));
		System.out.println(get("http://client.10010.com/clientT/query/queryRealFeeAndAccFee.do?versionCode=1.2&desmobile=6295E56026F92B9B49633B1049D4E0CDE718B1E6552666FE@E9B675FA6BA0148F47ABC7FF9F384A66&version=iphone_c@1.4.1&login=1"));
		System.out.println(get("http://client.10010.com/client/login.do?password=123456&version=android&mobile=18600660669"));
	}
}
